import { createRouter, createWebHistory } from 'vue-router'
import {getToken,removeToken} from '../utils/auth.js'
import{getInfo} from '../api/index.js'
const routes = [
  {
    path: '/',
    name: 'home',
    redirect: { name: 'homet' } ,
    component: () => import('../components/home/home.vue'),
    children:[ {
      path: 'Analysis',
      name: 'Analysis',
      component: () => import('../components/zhu/Analysis.vue')
    },
    {
      path: 'Search',
      name: 'Search',
      component: () => import('../components/zhu/Search.vue')
    },
    {
      path: 'homet',
      name: 'homet',
      component: () => import('../components/zhu/homet.vue')
    },]
  },{
    path: '/login',
    name: 'login',
    component: () => import('../components/login.vue')
  },
  
  {
    path: '/zhu',
    name: 'zhu',
    redirect: { name: 'shop' } ,
    component: () => import('../components/zhu/zhu.vue'),
    children:[ {
      path: 'shop',
      name: 'shop',
      component: () => import('../components/zhu/shop.vue')
    },
    
    
    {
      path: 'activationCode',
      name: 'activationCode',
      component: () => import('../components/zhu/activationCode.vue'),
    
    },
    {
      path: 'service',
      name: 'service',
      component: () => import('../components/zhu/service.vue'),
      children:[{
        path: 'authorization',
        name: 'authorization',
        component: () => import('../components/zhu/authorization.vue')
      },{
        path: 'Plugins',
        name: 'Plugins',
        component: () => import('../components/zhu/Plugins.vue')
      }]
    },
    {
      path: 'sales',
      name: 'sales',
      redirect: { name: 'salesBox' } ,
      component: () => import('../components/zhu/xiaosohou.vue'),
      children:[{
        path: 'salesBox',
        name: 'salesBox',
        
        component: () => import('../components/zhu/salesBox.vue')
      }]
    },
    {
      path: 'Customer',
      name: 'Customer',
      meta: { iskeepAlive: true},
      redirect: { name: 'news' } ,
      component: () => import('../components/zhu/Customer.vue'),
      children:[{
        path: 'news',
        name: 'news',
        meta: { iskeepAlive: true},
        component: () => import('../components/zhu/news.vue')
      }]
    },
    {
      path: 'count',
      name: 'count',
      meta: { iskeepAlive: true},
      redirect: { name: 'logistics' } ,
      component: () => import('../components/zhu/count.vue'),
      children:[{
        path: 'logistics',
        name: 'logistics',
        meta: { iskeepAlive: true},
        component: () => import('../components/zhu/logistics.vue')
      },{
        path: 'profit',
        name: 'profit',
        meta: { iskeepAlive: true},
        component: () => import('../components/zhu/profit.vue')
      }]
    },
    {
      path: 'industry',
      name: 'industry',
      meta: { iskeepAlive: true},
      redirect: { name: 'keyword' } ,
      component: () => import('../components/zhu/industry.vue'),
      children:[{
        path: 'keyword',
        name: 'keyword',
        meta: { iskeepAlive: true},
        component: () => import('../components/zhu/keyword.vue')
      },
      {
        path: 'category',
        name: 'category',
        meta: { iskeepAlive: true},
        component: () => import('../components/zhu/category.vue')
      },]
    },
    {
      path: 'order',
      name: 'order',
      meta: { iskeepAlive: true},
    
      component: () => import('../components/zhu/order.vue'),
   
    },
  ]
  }

 
]

const router = createRouter({
  history: createWebHistory(),
  routes
})
// 前置路由守卫
router.beforeEach((to, from, next) => {
 let bai=["home",'login']

  // if(bai.includes(to.name)) {
  //   next()
  // }


  if (!getToken() && !bai.includes(to.name)) next({name: "login"});
  // 用户已经登录
  // else if(getToken()&& to.name == "login") next({name:'home'});
 
   next()
});

// 后置路由守卫
router.afterEach((to, from) => {
  
// 如果是跳到登录页面，断开链接
  // if(to.name=="login"){
  //   removeToken()
 
  // //  scoket.disconnect()
  // }
  // sendToAnalytics(to.fullPath)
});

export default router
